/*
 * ArchiveEdit.java
 *
 * Created on 20.02.2005, 21:20
 * Copyright (c) 2006-2007, Daniel Apatin (ad), http://apatin.net.ru
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * You can also redistribute and/or modify this program under the
 * terms of the Psi License, specified in the accompanied COPYING
 * file, as published by the Psi Project; either dated January 1st,
 * 2005, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

package archive;

import Client.Msg;
import Client.StaticData;
import javax.microedition.lcdui.*;
import locale.SR;
//#ifdef CLIPBOARD
//# import Client.Config;
//# import util.ClipBoard; //<voffk>
//#endif

public class ArchiveEdit implements CommandListener {
    
    private Display display;
    //private Displayable parentView;
    private TextBox t;
    private String body;
    
//#ifdef CLIPBOARD
//#     private ClipBoard clipboard;
//#     private Command cmdPasteText=new Command(SR.MS_PASTE, Command.SCREEN, 98);
//#endif
    private Command cmdCancel=new Command(SR.MS_CANCEL, Command.BACK, 99);
    private Command cmdOk=new Command(SR.MS_OK, Command.OK /*Command.SCREEN*/, 1);
    
    private Msg msg;
    private boolean editMsg;
    MessageArchive archive;
    
    public ArchiveEdit(Display display, Msg msg, boolean editMsg) {
        this.display=display;
        this.msg=msg;
        this.editMsg=editMsg;
        //parentView=display.getCurrent();
        this.body=msg.getBody();
        
        int maxSize=500;
        t=new TextBox((editMsg)?SR.MS_EDIT:SR.MS_ADD, null, maxSize, TextField.ANY);
        
        try {
            //expanding buffer as much as possible
            maxSize=t.setMaxSize(4096); //must not trow
            
            if (body!=null) {
                //trim body to maxSize
                if (body.length()>maxSize)
                    body=body.substring(0, maxSize-1);
                t.setString(body);
            }
             
        } catch (Exception e) { }
        
        archive=new MessageArchive();
                
        t.addCommand(cmdOk);
        
//#ifdef CLIPBOARD
//#         if (Config.getInstance().useClipBoard) {
//#             clipboard=ClipBoard.getInstance();
//#             if (!clipboard.isEmpty())
//#                 t.addCommand(cmdPasteText);
//#         }
//#endif
        
        t.addCommand(cmdCancel);
        t.setCommandListener(this);
        display.setCurrent(t);
    }
    
//#ifdef CLIPBOARD
//#     public void insertText(String s, int caretPos) {
//#         
//#         String src=t.getString();
//#         
//#         StringBuffer sb=new StringBuffer(s);
//#         
//#         if (caretPos>0) 
//#             if (src.charAt(caretPos-1)!=' ')   
//#                 sb.insert(0, ' ');
//#         
//#         if (caretPos<src.length())
//#             if (src.charAt(caretPos)!=' ')
//#                 sb.append(' ');
//#         
//#         if (caretPos==src.length()) sb.append(' ');
//#         
//#         try {
//#             int freeSz=t.getMaxSize()-t.size();
//#             if (freeSz<sb.length()) sb.delete(freeSz, sb.length());
//#         } catch (Exception e) {}
//#         
//#         t.insert(sb.toString(), caretPos);
//#         
//#         sb=null;
//#     }
//#endif
    
    public void commandAction(Command c, Displayable d){
        body=t.getString();
        
//#if CLIPBOARD
//#         int caretPos=t.getCaretPosition();
//#         // +MOTOROLA STUB
//#         String platform=Info.Version.getPlatformName();
//#         if (platform.startsWith("Moto"))
//#             if (!platform.startsWith("Motorola-EZX")) caretPos=-1;
//#         // -MOTOROLA STUB
//#         
//#         if (caretPos<0) caretPos=body.length(); //<voffk: for clipboard only>
//#endif
        
        if (body.length()==0) body=null;
        
//#ifdef CLIPBOARD
//#         if (c==cmdPasteText) { insertText(clipboard.getClipBoard(), caretPos); return; }
//#endif
        
        Msg newmsg=null;
        if (c==cmdCancel) {
            if (editMsg)
                newmsg=new Msg(msg.messageType, msg.from, msg.subject, msg.getBody());
            else
                destroyView();
        }
        if (c==cmdOk) {
            newmsg=new Msg(msg.messageType, msg.from, msg.subject, body);
        }
        //archive.add(newmsg);
        archive.store(newmsg);
        archive.close();
        
        body=null;
        
        //new ArchiveList(display, null, -1);
        
        destroyView();
        return; 
    }
    
    public void destroyView(){
        display.setCurrent(StaticData.getInstance().roster);
    }
}